package com.magic.smartcity.view.more.hospital

import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.SeekBar
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.Navigation
import com.google.android.material.snackbar.Snackbar
import com.magic.smartcity.R
import com.magic.smartcity.beans.HospitalBanner
import com.magic.smartcity.databinding.FragmentHospitalDetailBinding
import com.magic.smartcity.utils.Constant
import com.magic.smartcity.view.MainActivity
import com.magic.smartcity.view.more.adapters.HospitalBannerAdapter
import com.magic.smartcity.view.more.viewmodels.HospitalViewModel

class HospitalDetailFragment : Fragment() {

    private lateinit var binding: FragmentHospitalDetailBinding
    private val list:List<HospitalBanner> = listOf()
    private val hospitalBannerAdapter=HospitalBannerAdapter(list)

    private val viewModel by lazy {
        ViewModelProvider(this).get(HospitalViewModel::class.java)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val id = arguments?.getInt("id")!!
        viewModel.getHospitalById(id)
        viewModel.getHospitalBanner(id)
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = FragmentHospitalDetailBinding.inflate(layoutInflater)

        initView()

        return binding.root
    }

    private fun initView() {

        //轮播图数据
        binding.banner3.setAdapter(hospitalBannerAdapter)
        viewModel.hospitalBannerList.observe(viewLifecycleOwner){
            hospitalBannerAdapter.setDatas(it)
        }

        //医院介绍
        viewModel.hospitalDetail.observe(viewLifecycleOwner){
            val mainActivity = activity as MainActivity
            mainActivity.setActionBarTitle(it.hospitalName)
            binding.webView2.loadData(it.brief,"text/html","utf-8")
            binding.hospitalDetail = it
        }

        //预约挂号点击
        binding.btnGuahao.setOnClickListener{
            Log.d("TAG","登录状态,====${Constant.TOKEN}")
            if (Constant.TOKEN==null) {
                val notLogin = Snackbar.make(binding.root, "未登录", Snackbar.LENGTH_SHORT)
                notLogin.setAction("去登陆"){ v->
                    Navigation.findNavController(it).navigate(R.id.loginFragment)
                }
                notLogin.show()
            }else{
                Navigation.findNavController(it).navigate(R.id.action_hospitalDetailFragment_to_hospitalPatientCardFragment)
            }
        }

    }

}